/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jimu.longkong.web.controller;

import com.jimu.base.misc.STRING;
import com.jimu.longkong.db.cache.Cache;
import com.jimu.longkong.db.domain.Impa;
import com.jimu.longkong.db.util.Pager;
import com.jimu.longkong.web.Web;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author mazhiqiang
 */
@Controller
public class ImpaController {

    @RequestMapping(value = "/impa/index", method = RequestMethod.GET)
    public ModelAndView index(Integer p) {
        if (p == null) {
            p = 1;
        }
        if (p < 1) {
            return Web.errpage("错误提示", "页面不存在");
        }
        Pager pager = Cache.impa.getPager(5, p);
        if (pager == null) {
            return Web.errpage("错误提示", "页面不存在");
        } else {
            return new ModelAndView("/impa/index.html")
                    .addObject("pager", pager);
        }
    }
    
    @RequestMapping(value = "/impa/item", method = RequestMethod.GET)
    public ModelAndView pack(Long id) {
        if (id == null || id <= 0) {
            return Web.errpage("错误提示", "页面不存在");
        } else {
            Impa impa = Cache.impa.get(id);
            if (impa == null) {
                return Web.errpage("错误提示", "记录不存在");
            }
            return new ModelAndView("/impa/item.html")
                    .addObject("impa", impa)
                    .addObject("message", "");
        }
    }
    

    @RequestMapping(value = "/impa/form", method = RequestMethod.GET)
    public ModelAndView form(Long id) {
        if (id == null || id <= 0) {
            Impa impa = new Impa();
            List<String> unitList = Cache.unit.getList();
            return new ModelAndView("/impa/form.html")
                    .addObject("impa", impa)
                    .addObject("unitTags", unitTags(unitList));
        } else {
            Impa impa = Cache.impa.get(id);
            if (impa == null) {
                return Web.errpage("错误参数", "不存在的Impa记录！");
            } else {
                List<String> unitList = Cache.unit.getList();
                // 处理 UNIT
                if (impa.getUnit() != null && !impa.getUnit().isEmpty()) {
                    for (String unit : impa.getUnit().split(",")) {
                        if (!Cache.unit.contains(unit)) {
                            unitList.add(unit);
                        }
                    }
                }
                return new ModelAndView("/impa/form.html")
                        .addObject("impa", impa)
                        .addObject("unitTags", unitTags(unitList));
            }

        }
    }
    
    private String unitTags(List<String> list) {
        String tags = "[";
        for (int i = 0; i < list.size(); i++) {
            if (i != list.size() - 1) {
                tags += "\"" + list.get(i) + "\", ";
            } else {
                tags += "\"" + list.get(i) + "\"]";
            }
        }
        return tags;
    }

    @RequestMapping(value = "/impa/insert", method = RequestMethod.POST)
    public ModelAndView insert(@ModelAttribute("Impa") Impa impa, BindingResult result) {
        if (impa == null) {
            return Web.errpage("错误提示", "页面不存在");
        }
        
        // 判断输入是否正确
        if (STRING.isNullOrEmpty(impa.getCode()) || STRING.isNullOrEmpty(impa.getName())) {
            return new ModelAndView("impa/save.html")
                    .addObject("ok", false)
                    .addObject("message", "编码和名称不能为空");
        }

        impa.setCode(impa.getCode().trim());
        impa.setName(impa.getName().trim());
        
        Impa i = Cache.impa.getBytheCode(impa.getCode());
        if (i != null) {
             return new ModelAndView("impa/save.html")
                    .addObject("ok", false)
                    .addObject("message", "具有相同编码的记录已存在！");
        }

        if (Cache.impa.insert(impa)) {
            return new ModelAndView("impa/save.html")
                    .addObject("ok", true)
                    .addObject("message", "创建成功")
                    .addObject("impa", impa);
        } else {
            return new ModelAndView("impa/save.html")
                    .addObject("ok", false)
                    .addObject("message", "创建失败，数据库操作错误！");
        }
    }
    
    @RequestMapping(value = "/impa/update", method = RequestMethod.POST)
    public ModelAndView update(@ModelAttribute("Impa") Impa impa, BindingResult result) {
        if (impa == null || impa.getId() <= 0) {
            return Web.errpage("错误提示", "页面不存在");
        }
        impa.setCode(impa.getCode().trim());
        impa.setName(impa.getName().trim());
        
        
        // 判断输入是否正确
        if (STRING.isNullOrEmpty(impa.getCode()) || STRING.isNullOrEmpty(impa.getName())) {
                return new ModelAndView("impa/save.html")
                        .addObject("ok", false)
                        .addObject("message", "名称不能为空");
        }
        
        Impa t = Cache.impa.get(impa.getId()); // 检查是否存在
        if (t == null || t.isDeleted()) {
                return new ModelAndView("impa/save.html")
                        .addObject("ok", false)
                        .addObject("message", "记录已被删除，不能修改。");
        } else {
            if (!impa.getCode().equals(t.getCode())) {
                // 保证Code唯一
                t = Cache.impa.getBytheCode(impa.getCode());
                if (t != null) {
                    return new ModelAndView("impa/save.html")
                            .addObject("ok", false)
                            .addObject("message", "编码冲突，相同编码的记录已存在！");
                }
            }
            if (Cache.impa.update(impa)) {
                return new ModelAndView("impa/save.html")
                        .addObject("ok", true)
                        .addObject("message", "修改成功")
                        .addObject("impa", impa);
            } else {
                return new ModelAndView("impa/save.html")
                        .addObject("ok", false)
                        .addObject("message", "修改失败，数据库操作错误！");
            }
        }
    }     
}